const pool = require('../../mysql/connection')
const UserService  = require('../../services/admin/UserService')
const JWT =require('../../util/JWT')
const fs = require('fs');
const path = require('path');


const UserController = {
    login:async (req,res)=>{
    let result =  await UserService.login(req.query);
            if(result==0){
                    res.send({
                        code:-1,
                        msg:'用户名或密码错误'
                    })
            }
            else if(result.code===0){
                res.send({
                    code:result.code,
                    err:result.err
                })
            }
            else{
                res.header('token',JWT.generate({result:result[0]},'1h'))
                res.send({
                    code:1,
                    result:{
                        username:result[0].username,
                        type: result[0].type,
                        avator: result[0].avator,
                        textarea: result[0].textarea
                    }
                })
            }},


            updata:async (req,res)=>{
                      const data = req.body
                    const token=  JWT.verify(req.get('token'))
                    const tokenresult =token.result
                    data.id=tokenresult.id
                    data.type=tokenresult.type
                    console.log(tokenresult);
                      if(data.password==='')
                            data.password=tokenresult.password
                    if(req.file){
                    data.avator = '/useravator/'+req.file.filename
                    }
                    else data.avator = tokenresult.avator
                    
                  
                    const code =  await UserService.updata(data)
                     if(code.code===400){
                        if(req.file)
                        fs.unlink(__dirname+'../../../public'+data.avator,(err)=>{
                            if(err)
                                console.log(err);
                            else console.log("修改成功");
                        }) 

                        res.send({
                            code:400,
                            result:code.err,
                            msg:'用户信息修改失败'
                         })
                     }
                     else {
                        res.header('token',JWT.generate({result:data},'1h'))
                        if(req.file)
                            fs.unlink(__dirname+'../../../public'+tokenresult.avator,(err)=>{
                                if(err)
                                    console.log(err);
                                else console.log("修改成功");
                            })
                                res.send({
                                    code:1,
                                    result:{
                                        username:data.username,
                                        type: data.type,
                                        avator: data.avator,
                                        textarea: data.textarea
                                    },
                                    msg:'用户信息修改成功'
                                })
                     }
                    
            },

            
            Useradd:async (req,res)=>{
                 const data = req.body
                 if(req.file)
                    data.avator = '/useravator/'+req.file.filename
                  else data.avator=null
                       
                 const result = await UserService.Useradd(data);
                 console.log(result);
                if(result.code===400)
                        res.send({
                            code:400,
                            result:result.err,
                            msg:"添加失败用户已经存在"
                        })
                 else
                        res.send({
                            code:1,
                            msg:"添加用户成功"
                        })
                    },
            Getalluser:async(req,res)=>{
                const result = await UserService.Getalluser()
                res.send({
                    code:1,
                    msg:'获取用户信息成功',
                    result:result
                })
            },
            Userdelete:async(req,res)=>{
                const result = await UserService.Userdelete(req.body.id)
                if(result.code==400){
                    res.send({
                        code:400,
                        result:result.err,
                        msg:'删除失败'
                    })
                }
                else{
                    res.send({
                        code:1,
                        msg:'删除成功'
                    })
                }
            },
            Useredit:async(req,res)=>{
                const result = await UserService.Useredit(req.body)
                if(result.code===400){
                    res.send({
                        code:400,
                        result:result.err,
                        msg:'修改失败用户名已存在'
                    })
                }
                else {
                    res.send({
                        code:1,
                        result:result,
                        msg:'修改成功'
                    })
                }
            }
        }


module.exports = UserController